import { get,post } from './request'

const createSmyooApi = ({mcuName, mcuid, status, text})=>{
  class smyooApiCenter {
    constructor() {
			this.mcuName = mcuName
			this.mcuid = mcuid
			this.status = status
			this.text = text
		}
     immediateApi = async()=> {
      const BpeSessionId = wx.getStorageSync('BpeSessionId')
      const thisTime = new Date().getTime()
      const ExpiresTime = wx.getStorageSync('ExpiresTime') || 0
      if(!BpeSessionId || thisTime >= ExpiresTime){
        await this.apiSynLoginOpen()
      }
      // else{
      //   // 控制开关
      //   this.apiSetDevicedata()
      // }
    }
     apiSynLoginOpen = async()=>{
      console.log('--------------')
      const {data} = await post('https://auth.smyoo.com/v1/account/synloginopen', { 
            appId: 1314,
            endpointOS: 1,
            phone: '18510190034',
            password: 'Lh12112114',
            autologin: 'true',
            deviceId: 'F519CC4D4AAF48928456D2B5D9696D2B',
            client_id: '86921933',
            client_secret: 'DC4DD36876E74D75B5F6B02E9A122E67',
       })
      console.log('synloginopen-data---', data)
      await this.apiSynLoginTicket(data.ticket)

    }
    apiSynLoginTicket = async (ticket) => {
      const {data} = await post('https://auth.smyoo.com/api/gfriend/synloginticket', {
          appId: 1314,
          endpointOS: 1,
          ticket: ticket,
          deviceId: 'F519CC4D4AAF48928456D2B5D9696D2B',
          client_id: '86921933',
          client_secret: 'DC4DD36876E74D75B5F6B02E9A122E67',
      }) 
      console.log('synloginticket-data---', data)
      const { BpeSessionId } = data
      // 这里需要把 BpeSessionId set到cookie里
      // 后面接口 header 都需要传 cookie
      wx.setStorageSync('BpeSessionId', BpeSessionId)
      // 设置24小时过期时间
      const ExpiresTime = new Date().getTime() + 86400000
      wx.setStorageSync('ExpiresTime', ExpiresTime)
    }
    // 获取我的设备列表
    apiQueryDevices = async () => {
      const {data} = await post('https://auth.smyoo.com/api/gfriend/querydevices', {}) 
      console.log('querydevices-data---', data);
      return data.mcuinfos;
    }
    // 获取单个设备状态
    apiGetDevicedata = async () => {
      const { mcuid } = this
      const {data} = await post('https://auth.smyoo.com/api/gfriend/getdevicedata', {
        mcuid
      }) 
      console.log('apiGetDevicedata-data---', data)
    }


    // 设置设备开关
    apiSetDevicedata = async ()=>{
      const { mcuid, status } = this;
      await post('https://auth.smyoo.com/api/gfriend/setdevicedata', {
        appId: 1314,
        mcuid: mcuid,
        datatype: 1,
        datapoint: JSON.stringify({status}),
      })
    }
    // 设置后红外空调开关 Ir 红外 infra-red 
    apiSetIrDevicedata = async ()=>{
      const { mcuid, status } = this
      await post('https://auth.smyoo.com/api/gfriend/irdevicesetdata', {
        appId: 1314,
        mcuid: mcuid,
        datatype: 1,
        datapoint: JSON.stringify({key: 0, status}),
      })
    }
    // 小喇叭播放语音
    apiSpeakerPlayText = async () => {
      const { mcuid, text } = this
      await post('https://auth.smyoo.com/api/gfriend/speakerplaytext', {
        appId: 1314,
        mcuid: mcuid,
        datatype: 1,
        datapoint: JSON.stringify({text}),
      })
    }
  }
  return new smyooApiCenter();
}

export default createSmyooApi